package com.servlet.music;

import com.dao.SongListService;
import com.dao.impl.SongListServiceImpl;
import com.entity.Member;
import com.entity.SongList;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.servlet.teacher.BaseServlet;
import com.util.JsonUtil;
import com.util.PageUtil;
import com.util.ProxyUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.SqlDateConverter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

@WebServlet("/SongSheetServlet")
public class SongSheetServlet extends BaseServlet {
    SongListService songList = (SongListService) ProxyUtil.getDaoProxy(new SongListServiceImpl());

    /**
     * 查询所有数据
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void queryList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码
        resp.setContentType("text/html;charset=utf-8");
        SongList list=new SongList();
        //当前页
        String currentPage = req.getParameter("currentPage");
        //获取每页显示的数量
        String pageSize = req.getParameter("pageSize");
        //查询条件
        String  title= req.getParameter("title");
        list.setTitle(title);
        System.out.println("收到请求");
        JsonUtil jsonUtil = new JsonUtil();
        try {
            PageUtil<SongList> songLists = songList.queryDataBypage(Integer.parseInt(currentPage), Integer.parseInt(pageSize), list);
            jsonUtil.setCode(200);
            jsonUtil.setData(songLists);
        } catch (Exception e) {
            jsonUtil.setCode(500);
            jsonUtil.setMsg("系统错误");
            e.printStackTrace();
        }

        Gson gson = new Gson();
        String toJson = gson.toJson(jsonUtil);

        PrintWriter writer = resp.getWriter();
        writer.print(toJson);
    }

    //删除单行
    public void deleteRow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");

        String id = req.getParameter("songSheetId");
        JsonUtil jsonUtil = new JsonUtil();
        try {
            int res = songList.deleteById(id);
            jsonUtil.setCode(200);
            jsonUtil.setMsg("成功");
        } catch (SQLException e) {
            jsonUtil.setCode(500);
            jsonUtil.setMsg("失败");
            e.printStackTrace();
        }
        Gson gson = new Gson();
        String toJson = gson.toJson(jsonUtil);

        PrintWriter writer = resp.getWriter();
        writer.print(toJson);
    }

    //批量删除
    public void deleteByIdS(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String ids = req.getParameter("ids");

        Gson gson = new Gson();
        JsonUtil jsonUtil = new JsonUtil();
        List<Object> o = gson.fromJson(ids, new TypeToken<List<Object>>() {
        }.getType());

        try {
            int i = songList.deleteByIdS(o);
            jsonUtil.setCode(200);
            jsonUtil.setMsg("成功");
        } catch (SQLException e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("失败");
        }
        String toJson = gson.toJson(jsonUtil);

        PrintWriter writer = resp.getWriter();
        writer.print(toJson);
    }

    // 修改
    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ConvertUtils.register(new SqlDateConverter(null), java.sql.Date.class);
        SongList list = new SongList();
        req.removeAttribute("");

        //日期格式转换器yyyy-MM-dd转换成data类型(很重要)
        ConvertUtils.register(new DateLocaleConverter(), Date.class);

        Gson gson = new Gson();
        JsonUtil jsonUtil = new JsonUtil();

        PrintWriter writer = resp.getWriter();
        try {
            BeanUtils.populate(list, req.getParameterMap());

            List<SongList> lists = songList.queryData(list);

            songList.update(list);
            jsonUtil.setCode(200);
            jsonUtil.setMsg("成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonUtil.setCode(500);
            jsonUtil.setMsg("失败");
        }
        String toJson = gson.toJson(jsonUtil);
        writer.print(toJson);
    }

    // 增加
    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SongList list = new SongList();

        //日期格式转换器yyyy-MM-dd转换成data类型(很重要)
        ConvertUtils.register(new DateLocaleConverter(), Date.class);

        Gson gson = new Gson();
        JsonUtil jsonUtil = new JsonUtil();
        PrintWriter writer = resp.getWriter();
        try {
            BeanUtils.populate(list, req.getParameterMap());
            List<SongList> lists = songList.queryData(list);

            if (lists.size() != 0) {
                jsonUtil.setCode(111);
                String str = gson.toJson(jsonUtil);
                writer.print(str);
                return;
            }
            songList.insert(list);
            jsonUtil.setCode(200);
            jsonUtil.setMsg("成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonUtil.setCode(500);
            jsonUtil.setMsg("失败");
        }
        String toJson = gson.toJson(jsonUtil);


        writer.print(toJson);
    }
}

